package com.interview.ujiuye4;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * Copyright (C), 2018-2020
 * FileName: 六个矩形长方体
 * Author:   kongfanyu
 * Date:     2020/10/20 14:33
 * https://blog.csdn.net/sinat_40380134/article/details/105605585
 * 12个数中只有3个数，且每个数重复4次这一条件，就能构成长方体
 */
public class 六个矩形长方体 {
    public static void main(String[] args) {
        int [][] rectangles = { {5,10},{5,10},{5,10},{5,10},{3,4},{3,4} };
        if (isCuboid(rectangles)){
            System.out.println("可以构造长方体");
        }else{
            System.out.println("不能可以构造长方体");
        }
    }
    public static boolean isCuboid(int [][] rectangles){
        //把12个数放到List集合中
        List<Integer> list = new ArrayList<>();
        for (int i=0;i<rectangles.length;i++){
            for (int j = 0; j < rectangles[i].length; j++) {
                list.add(rectangles[i][j]);
            }
        }
        // 定义一个HashMap来统计个数
        HashMap<Integer,Integer> map = new HashMap<>();
        for (int i = 0,j = list.size(); i < j; i++) {
            Integer tempInt =map.get(list.get(i)) ;
            if (tempInt == null){
                tempInt = 1;
            }else{
                tempInt++;
            }
            map.put(list.get(i),tempInt);
        }
        System.out.println(map);
        //判断是否长方体
        Set<Integer> keySet = map.keySet();


        for (Integer k : keySet){
            Integer v = map.get(k);
            if (v == 4){
                return true;
            }
        }
        return false;
    }
}
